$(function () {
    BASE.COMMON.getStatus(BASE.COMMON.getId(),1,function () {
        BASE.VIDEO_PLAY.isbuyCode();
    });
    BASE.VIDEO_PLAY.getBbsList(1);
    if( ($('#content').data('price')>0 && BASE.COMMON.getUserId()) || $('#content').data('price')<=0){
        $('#content .video-content').trigger('click');
    }
});

var  BASE = BASE||{}; //全局公用方法
BASE.VIDEO_PLAY = (function () {
    function iniPage() {
        var currentLink = document.querySelector('#catalog .link.hover');
        var h = currentLink.offsetTop - $('#catalog').height()/2;
        $('#catalog').animate({scrollTop: h},100);
    }
   /* 获取视频id*/
    function _getVideoId() {
        return {
            videoInfoId:BASE.COMMON.getId(),
        }
    }
    /*源码是否购买*/
    function isbuyCode() {
        var sendData = {
            relatedId: BASE.COMMON.getId(),
            type: 2, //1,视频,2代码,3bbs,4线上班,5源码
        };
        BASE.apiGet('/device/mstatus/detail', sendData, function (res) {
            if(!res){
               return;
            }
            if (res.buyIs) {
                $('#buyIs span:eq(0)').hide();
                $('#buyIs span:eq(1)').show();
            }
        });
    }
    /***********************************************评论部分stat******************************/
    /*点赞*/
    function zanVideo(obj) {
        BASE.apiPost('/device/videoInfo/like',_getVideoId(),function () {
            $(obj).hide().next().fadeIn();
        })
    }
    /*取消点赞*/
    function cancelzanVideo(obj) {
        BASE.apiPost('/device/videoInfo/unlike',_getVideoId(),function () {
            $(obj).hide().prev().fadeIn();
        })
    }
    
    /***********************************************评论部分end******************************/
    /***********************************************作业start******************************/
    /*获取作业列表*/
    function getBbsList(page) {
        var type = $('#box').data('type');
        var $this = $(this);
        var flag = $this.data('flag');
        var sendData={
            relatedId:BASE.COMMON.getId(),
            pageNum : page,
            pageSize:15,
            type:type,
        };
        if(flag && $this[0].toString()!=='[object Window]'){
            return;
        }
        BASE.apiGet('/device/bbs/list',sendData,function (res) {
            $this.data('flag',true);
            if(res.list.length>0){
                $('#taskList').html(template('taskList_tpl',{data:{
                    taskList:res.list,
                    userId:BASE.COMMON.getUserId(),
                }}));
                paging(res.pageNum,res.pages,getBbsList,'task');
            }
           /* BASE.UPLOAD.ue.setContent('请输入作业答案...(10字以上，附件只能上传压缩包)');*/
        })
    }
    /*添加作业列表*/
    function addBbs(type,title,categoryId) {
      /*  var ubb = UBB.toUbb(BASE.UPLOAD.ue.getContent());*/
        var ubb =BASE.UPLOAD.ue.txt.html();
        var sendData={
            relatedId:BASE.COMMON.getId(),
            content:ubb,
            attachUrl:$('#uploadFile').data('url'),
            title:title,
            categoryId:categoryId,
            type:type,//类型:1作业,2招聘,3公告
        };
        if (!sendData.content||sendData.content.length<10) {
            BASE.clearpop('内容不能少于10字');
            $('#task_content').focus();
            return;
        }
        BASE.apiPost('/device/bbs/add',sendData,function () {
            BASE.clearpop('提交成功');
            BASE.UPLOAD.ue.txt.html('');
            $('#uploadFile a').trigger('click');
            getBbsList(1);
        })
    }
    /*源码购买*/
    function buyCode(obj) {
        $(obj).attr('disabled',true).text('提交中...');
        BASE.postHttp('/device/order/code/buy',{videoInfoId:BASE.COMMON.getId()},function (res) {
            if(res.code!=200){
                $(obj).attr('disabled',false).text('重新提交');
                BASE.clearpop(res.message);
                return;
            }
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        });
    }
    /*视频购买*/
    function buyVideo(obj) {
        $(obj).attr('disabled',true).text('提交中...');
        BASE.postHttp('/device/order/videoInfo/buy',{videoInfoId:BASE.COMMON.getId()},function (res) {
            if(res.code!=200){
                $(obj).attr('disabled',false).text('重新提交');
                BASE.clearpop(res.message);
                return;
            }
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        });
    }
    if(BASE.COMMON.getUserId()) {
        /*视频分享后*/
        $('body').on('click', '.share-item', function () {
            BASE.postHttp('/device/videoInfo/share', _getVideoId());
        });
        $('body').on('mouseenter', '.icon-wechat', function () {
            BASE.postHttp('/device/videoInfo/share', _getVideoId());
        });
    }
    /*评论和作业滚动*/
    function scrollTo(id) {
        $('#'+id).click();
        setTimeout(function () {
            BASE.COMMON.scrollTop($('#content').height(),400);
        },200);
    }
    /*目录显示切换*/
    function toggleCatalog() {
        event.stopPropagation();
        $(this).toggleClass('hover');
        $('#catalog').toggleClass('hover');
    }
    /*视频播放*/
    function videoJump() {
        event.stopPropagation();
        var href = $(this).data('href');
        $('#catalog_nav').removeClass('hover');
        $('#catalog').removeClass('hover');
        location.href =  href;
    }
    $('body').on('click',function () {
        $('#catalog_nav').removeClass('hover');
        $('#catalog').removeClass('hover');
    });
    return{
        iniPage:iniPage,
        isbuyCode:isbuyCode,
        zanVideo:zanVideo,
        cancelzanVideo:cancelzanVideo,
        getBbsList:getBbsList,
        addBbs:addBbs,
        buyCode:buyCode,
        buyVideo:buyVideo,
        scrollTo:scrollTo,
        toggleCatalog:toggleCatalog,
        videoJump:videoJump,
    };
    /***********************************************作业end******************************/
})();

